﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using FileSystemControls.Support;

namespace FileSystemControls.ListViewItems
{
    public class DotDotListViewItem : FileSystemListViewItem
    {
        protected DirectoryInfo _thisDirectory;

        public DotDotListViewItem(DirectoryInfo CurrentEntry)
        {
            IntPtr iconHandle; //the handle to the system image list
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();

            //Use this to get the small Icon
            iconHandle = NativeMethods.SHGetFileInfo(CurrentEntry.FullName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_TYPENAME);
            _smallIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

            if (CurrentEntry.Exists)
            {
                _isFile = false;

                SubItems.Add("<DIR>");
                Path = CurrentEntry.FullName;

                _fullPath = CurrentEntry.FullName;
                ((FileSystemListViewItem) this).RootName = CurrentEntry.Name;
                _thisDirectory = CurrentEntry;
                _isReadOnly = false;

                Name = CurrentEntry.Name;
                _createDate = CurrentEntry.CreationTime;
                _modifyDate = CurrentEntry.LastWriteTime;

                Text = "..";
                SubItems.Add("..");

            }
            else
            {
                throw new DirectoryNotFoundException();
            }

        }
    }
}
